package cn.wq3stone.web;

import cn.wq3stone.exception.PasswordNullException;
import cn.wq3stone.exception.RegisterException;
import cn.wq3stone.service.UserService;
import cn.wq3stone.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author cn.wq3stone
 */

public class RegisterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UserService userService = new UserServiceImpl();
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String password2 = req.getParameter("password2");
        try {
            userService.register(username,password,password2);
            out.println("测试Success register " + username);
        } catch (PasswordNullException e) {
            out.println("Password can't is null测试");
        } catch (RegisterException e) {
            out.println("Register failed测试");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
